package wechat.bean.request.message;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import wechat.resources.AppFilter;
import wechat.resources.InstanceResource;
import wechat.service.base.WechatApi;

/**
 * 企业号
 * 
 */
public class NewsBean extends ActiveMessageBaseBean implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private List<String> touser;
	private List<String> toparty;
	private List<String> totag;
	private String msgtype;
	private String agentid;
	private String text;
	private List<ArticleBean> articles;

	public NewsBean() {
		this.msgtype = ActiveMessageBaseBean.Message_News;
	}

	public List<String> getTouser() {
		return touser;
	}

	public void setTouser(List<String> touser) {
		this.touser = touser;
	}

	public List<String> getToparty() {
		return toparty;
	}

	public void setToparty(List<String> toparty) {
		this.toparty = toparty;
	}

	public List<String> getTotag() {
		return totag;
	}

	public void setTotag(List<String> totag) {
		this.totag = totag;
	}

	public String getMsgtype() {
		return msgtype;
	}

	public void setMsgtype(String msgtype) {
		this.msgtype = msgtype;
	}

	public String getAgentid() {
		return agentid;
	}

	public void setAgentid(String agentid) {
		this.agentid = agentid;
	}

	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public List<ArticleBean> getArticles() {
		return articles;
	}

	public void setArticles(List<ArticleBean> articles) {
		this.articles = articles;
	}

	@Override
	public String toString() {
		return "NewsBean [touser=" + touser + ", toparty=" + toparty + ", totag=" + totag + ", msgtype=" + msgtype
				+ ", agentid=" + agentid + ", text=" + text + ", articles=" + articles + "]";
	}

	public String toWechatString() throws Exception {
		JSONObject jsonObject = new JSONObject();
		if (this.touser == null || this.touser.size() <= 0) {
			throw new Exception("接受者不能为空");
		}
		if (this.articles == null || this.articles.size() <= 0) {
			throw new Exception("图文消息不能为空");
		}
		if (this.agentid == null || this.agentid.equals("")) {
			throw new Exception("企业应用的id不能为空");
		}

		String touserVar = "";
		for (int i = 0; i < this.touser.size(); i++) {
			if (i == 0) {
				touserVar = this.touser.get(i);
			} else {
				touserVar = touserVar + "|" + this.touser.get(i);
			}
		}
		jsonObject.put("touser", touserVar);

		if (this.toparty != null && this.toparty.size() > 0) {
			String topartyVar = "";
			for (int i = 0; i < this.toparty.size(); i++) {
				if (i == 0) {
					topartyVar = this.toparty.get(i);
				} else {
					topartyVar = topartyVar + "|" + this.toparty.get(i);
				}
			}
			jsonObject.put("toparty", topartyVar);
		}

		if (this.totag != null && this.totag.size() > 0) {
			String totagVar = "";
			for (int i = 0; i < this.totag.size(); i++) {
				if (i == 0) {
					totagVar = this.totag.get(i);
				} else {
					totagVar = totagVar + "|" + this.totag.get(i);
				}
			}
			jsonObject.put("totag", totagVar);
		}

		JSONArray articlesArray = new JSONArray();
		for (int i = 0; i < this.articles.size(); i++) {
			ArticleBean articleBean = this.articles.get(i);
			articlesArray.add(articleBean);
		}
		JSONObject art = new JSONObject();
		art.put("articles", articlesArray);
		
		
		jsonObject.put("msgtype", this.msgtype);
		jsonObject.put("agentid", this.agentid);
		jsonObject.put("news", art);
		
		
		return jsonObject.toJSONString();
	}

	public String toAllUserWechatString() throws Exception {
		JSONObject jsonObject = new JSONObject();
		if (this.agentid == null || this.agentid.equals("")) {
			throw new Exception("企业应用的id不能为空");
		}

		jsonObject.put("touser", "@all");

		if (this.toparty != null && this.toparty.size() > 0) {
			String topartyVar = "";
			for (int i = 0; i < this.toparty.size(); i++) {
				if (i == 0) {
					topartyVar = this.toparty.get(i);
				} else {
					topartyVar = topartyVar + "|" + this.toparty.get(i);
				}
			}
			jsonObject.put("toparty", topartyVar);
		}

		if (this.totag != null && this.totag.size() > 0) {
			String totagVar = "";
			for (int i = 0; i < this.totag.size(); i++) {
				if (i == 0) {
					totagVar = this.totag.get(i);
				} else {
					totagVar = totagVar + "|" + this.totag.get(i);
				}
			}
			jsonObject.put("totag", totagVar);
		}

		JSONArray articlesArray = new JSONArray();
		for (int i = 0; i < this.articles.size(); i++) {
			ArticleBean articleBean = this.articles.get(i);
			articlesArray.add(articleBean);
		}
		JSONObject art = new JSONObject();
		art.put("articles", articlesArray);
		
		
		jsonObject.put("msgtype", this.msgtype);
		jsonObject.put("agentid", this.agentid);
		jsonObject.put("news", art);
		
		
		return jsonObject.toJSONString();
	}

	public static void main(String[] args) throws Exception {
		InstanceResource instanceResource = AppFilter.chooseApp("53");
		String access_token = "OHy3lmanmEAGPn8sYqDV3NjBo7ok7WNgE8pItQtZjfcTMH_nlaCQEJSxCmhHZAwu";
		String url = WechatApi.getSend_message(access_token);

		 List<String> to = new ArrayList<String>();
		 to.add("zhang");
		 //to.add("scrzxx");
		 
		 ArticleBean bean = new ArticleBean();
		 ArticleBean bean2 = new ArticleBean();
		 
		 bean.setTitle("1");
		 bean.setDescription("1");
		 bean.setPicurl("1");
		 bean.setUrl("1");
		 
		 bean2.setTitle("2");
		 bean2.setDescription("2");
		 bean2.setPicurl("2");
		 bean2.setUrl("2");
		 
		 List<ArticleBean> articles = new ArrayList<ArticleBean>();
		 articles.add(bean);
		 articles.add(bean2);
		
		 NewsBean newsBean = new NewsBean();
		 newsBean.setAgentid("53");
		 newsBean.setText("你好，hello。哈哈");
		 newsBean.setTouser(to);
		 newsBean.setArticles(articles);
		 
		 
		 String param = newsBean.toWechatString();
		 System.out.println(param);
//		 String result = HttpClientUtil.post(url, param);
//		 JSONObject jsonResult = JSONObject.parseObject(result);
//		 System.out.println(jsonResult);
	}

}
